//
//  HZEmoStackView.swift
//  weibo002
//
//  Created by 黑眼圈 on 16/4/26.
//  Copyright © 2016年 heiyan. All rights reserved.
//

import UIKit


//按钮类型的枚举
enum HZEmoStackViewBtnType:Int{
    
    case nowBtnType = 0 , defaultBtnType , emojiBtnType , lxhBtnType
    
}



class HZEmoStackView: UIStackView {
    
    
    //定义按钮点击的闭包
    var didBtn:((btnType:HZEmoStackViewBtnType)->())?

   
    //记录按钮
    var btn:UIButton?
    
    
    var seleIndex:Int?{
        
        
        didSet{
            
            //通过 tag值取出对应的按钮
           let button = viewWithTag(seleIndex ?? 0) as! UIButton
            
            btn?.selected = false
            button.selected = true
            btn = button
        }
        
    }
    
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //添加子控件
    private func setupUI(){
        
        backgroundColor = UIColor.orangeColor()
        
        //设置均等分
        distribution = .FillEqually
        
        addChiledBtn("最近", btnType: .nowBtnType)
        addChiledBtn("默认", btnType: .defaultBtnType)
        addChiledBtn("Emoji", btnType: .emojiBtnType)
        addChiledBtn("浪小花", btnType: .lxhBtnType)
        
        
    }
    
    
    
    
    
    
    
    private  func addChiledBtn(text:String,btnType:HZEmoStackViewBtnType){
    
        let btn = UIButton()
        
        //监听按钮点击事件
        btn.addTarget(self, action: "didClick:", forControlEvents: .TouchUpInside)
        
        //设置按钮的 tag值(取出枚举的原始值)
        btn.tag = btnType.rawValue
        
        btn.titleLabel!.font = UIFont.systemFontOfSize(14)
        btn.setTitle(text, forState: .Normal)
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Selected)
        
        btn.setBackgroundImage(UIImage(named:"compose_emotion_table_mid_normal"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named:"compose_emotion_table_left_selected"), forState: .Selected)
        
        addArrangedSubview(btn)
    
    }
    
    
    // MARK: -  按钮点击事件
    @objc private func didClick(btn:UIButton){
        
        
        //执行闭包
        didBtn!(btnType:HZEmoStackViewBtnType(rawValue: btn.tag)!)
        
        self.btn?.selected = false
        btn.selected = true
        self.btn = btn
        
        
    
        
    }
    
    
    

}
